package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Tag;
import com.itheima.ydd.service.TagService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//标签管理
@RestController
@RequestMapping("/ydd_tag")
public class TagController {
    @Autowired
    private TagService tagService;

    //标签管理-分页查询
    @GetMapping("/list")
    public ResultData list(String tagname, int page, int limit){

        Page<Tag> tagPage = new Page<>(page, limit);

        LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(tagname),Tag::getTagname,tagname);

        tagService.page(tagPage,lqw);


        //返回总页数，当前页大小，当前页，记录
        return ResultData.ok("", PageResult.init(tagPage.getTotal()
                ,tagPage.getSize()
                ,tagPage.getCurrent()
                ,tagPage.getRecords()));
    }

   //标签管理-添加标签
    @PostMapping("/add")
    public ResultData add(@RequestBody Tag tag){
        tag.setDefault_data("1");

        tagService.save(tag);
        return ResultData.ok("","标签添加成功！");
    }

    //标签管理-根据Id查询标签信息
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Long id){
        Tag tag = tagService.getById(id);
        return ResultData.ok("",tag);
    }

    //标签管理-删除标签
    @GetMapping("/del")
    public ResultData delete(Long id){

        tagService.removeById(id);
        return ResultData.ok("","标签删除成功！");
    }

    //标签管理-修改标签信息
    @PostMapping("/update")
    public ResultData update(@RequestBody Tag tag){

        tagService.updateById(tag);
        return ResultData.ok("","标签修改成功");
    }

    //标签管理-查询所有标签信息
    @GetMapping("/all")
    public ResultData all(){
        List<Tag> list = tagService.list();
        return ResultData.ok("",list);
    }
}
